package org.kgc.alanhealth.controller;

import org.kgc.alanhealth.entity.HospitalShoppingcart;
import org.kgc.alanhealth.service.ShoppingCarService;
import org.kgc.alanhealth.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("cart")
@CrossOrigin(origins = "*")
public class ShoppingCarController {
    @Autowired
    ShoppingCarService shoppingCarService;

    @PostMapping("add")
    public Result addShoppingCar(@RequestBody HospitalShoppingcart shoppingcart){
        if(shoppingCarService.insertSelective(shoppingcart)){

            return Result.success("加入购物车成功");
        }else {
            return Result.error("加入购物车失败");
        }

    }
    @PostMapping("change")
    public Result changeStatus(@RequestBody List<HospitalShoppingcart> shoppingcarts){
        if(shoppingCarService.updateStatusById(shoppingcarts)){
            return Result.success("更新状态成功");
        }else {
            return Result.error("更新状态失败");
        }
    }



    @RequestMapping("select")
    public Result selectById(int id){
        List<HospitalShoppingcart> list=shoppingCarService.selectByUserid(id);
        if (list!=null&&!list.isEmpty()){
            return Result.success(list);
        }else{
            return Result.error("没有找到数据");
        }
    }
    @RequestMapping("delete")
    public Result deleteById(long id){
        if(shoppingCarService.deleteByPrimaryKey(id)){
            return Result.success("删除成功");
        }else{
            return Result.error("删除失败");
        }
    }

}
